package com.sports.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.sports.entity.Comment;
import com.sports.entity.CommentDTO;
import com.sports.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping
    public Map<String, Object> getComment(String athletesName) {
        HashMap<String, Object> map = new HashMap<>();
        // if (StrUtil.isEmpty(athletesName)) {
        //     map.put("data", commentService.getCommentsByAthletesName(null));
        // } else {
        //     map.put("data", commentService.getCommentsByAthletesName(athletesName));
        // }
        map.put("data", commentService.getCommentsByAthletesName(athletesName));
        map.put("code", 200);
        map.put("message", "success");
        return map;
    }

    @PostMapping
    public Map<String, Object> addComment(@RequestBody Comment comment) {
        System.err.println("comment = " + comment);
        HashMap<String, Object> map = new HashMap<>();
        if (ObjUtil.isEmpty(comment)) {
            map.put("code", 400);
            map.put("message", "参数错误");
        } else {
            boolean save = commentService.saveOrUpdate(comment);
            if (save) {
                map.put("code", 200);
                map.put("message", "success");
            } else {
                map.put("code", 400);
                map.put("message", "添加失败");
            }
        }
        return map;
    }

    @DeleteMapping("/{commentsId}")
    public Map<String, Object> deleteComment(@PathVariable("commentsId") Integer commentsId) {
        HashMap<String, Object> map = new HashMap<>();
        if (commentsId != null) {
            boolean isRemove = commentService.removeById(commentsId);
            if (isRemove) {
                map.put("code", 200);
                map.put("message", "success");
            } else {
                map.put("code", 400);
                map.put("message", "删除失败");
            }
        } else {
            map.put("code", 400);
            map.put("message", "删除失败");
        }
        return map;
    }
}
